package tp.pr4.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import tp.pr4.Place;
import tp.pr4.RobotEngine;
import tp.pr4.items.Item;
import tp.pr4.items.ItemContainer;
import tp.pr4.listeners.AbstractRobotListener;
import tp.pr4.util.Vec_thor;

public class RobotInfo extends JPanel{
	private static final long serialVersionUID = 1L;
	private final JTable table;
	/**
	 * Creates the robot info, adding a new ponts and status label, and creating a new JTable to hold the items
	 * @param r
	 */
	public RobotInfo(final RobotEngine r) {
		this.setLayout(new BorderLayout());
		this.setBorder(new TitledBorder("Robot Info"));
		this.add(new RobotFuelAndPointsStatus(r), BorderLayout.NORTH);
		
		final DefaultTableModel model = new DefaultTableModel(); 
		model.addColumn("Item ID"); 
		model.addColumn("Item Description"); 
		this.table = new JTable(model) {
			private static final long serialVersionUID = 1L;
			{
				r.addRobotListener(new AbstractRobotListener() {
					@Override
					public void onInventoryChanged(ItemContainer inventory, Place where) {
						int rC = model.getRowCount();
						for (int i = 0; i < rC; i++) 
							model.removeRow(0);
						Vec_thor<Item> v = inventory.toVec_thor();
						for (int i = 0; i < v.size(); i++) 
							model.addRow(new Object[]{v.get(i).getId(),v.get(i).toString()});
					}
				});
			}	
		};
		
		this.add(new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER) {
			private static final long serialVersionUID = 1L;
			{	
				this.setPreferredSize(new Dimension(0, 0)); //así se limita a ocupar lo que le dejen		
			}
		},BorderLayout.CENTER);
	}

	public JTable getJTable() {
		return this.table;
	}
	
}
